var defaultParams;
// var isDelever = true; //是否可分发  默认可分发
var menuId; //菜单栏父元素ID
var tableData;
// 详情
function detail() {
    if (defaultParams) {
        modifyVsdetailData()
        $("#buttonDetail").hide()
        $("#buttonBack").show()
        $(".buttonSearchBox").hide()
        $(".bootstrap-table").eq(0).hide()
        $(".bootstrap-table").eq(1).show()
        $(".contentInput").show()
    } else {
        layer.msg('请选择一项查看');
    }
}

//返回
function back() {
    $("#buttonDetail").show()
    $("#buttonAdd").show()
    $("#buttonModify").show()
    $("#buttonDelete").show()
    $("#buttonBack").hide()
    $(".buttonSearchBox").show()
    $(".bootstrap-table").eq(0).show()
    $(".bootstrap-table").eq(1).hide()
    $(".contentInput").hide()
    defaultParams = ""
    $("#table").bootstrapTable('refresh');

}

// 修改和详情数据
function modifyVsdetailData() {
    $.ajax({
            type: "get",
            url: "/operation/event/pending/info",
            dataType: "json",
            data: { "eventId": defaultParams.eventId },
            cache: false,
            success: function(msg) {
                $("#eventCode").val(msg.data.eventId)
                $("#subbmitMan").val(msg.data.insertUserName)
                $("#phone").val(msg.data.tel)
                $("#organiza").val(msg.data.orgName)
                $("#eventTitle").val(msg.data.eventTheme)
                $("#eventDesc").val(msg.data.eventDetail)
            },
            error: function(e) {
                if (e.status == 500) {
                    layer.msg("系统错误");
                    return
                } else if (e.responseText) {
                    var errorMessage = JSON.parse(e.responseText).errors[0].message;
                    layer.msg(errorMessage);
                }
            }
        })
        // 系统通道和问题类别
    $.ajax({
        type: "get",
        url: "/operation/channel/getChannelByMenu",
        dataType: "json",
        contentType: 'application/json',
        data: { "menuId": menuId },
        cache: false,
        success: function(msg) {
            systermRouteData("#systermRoute", msg.data)
            $.ajax({
                type: "get",
                url: "/operation/question/getQuestionByMenuAndChannel",
                dataType: "json",
                contentType: 'application/json',
                data: { "menuId": menuId, "channelId": $("#systermRoute option:selected").attr("channelId") },
                cache: false,
                success: function(msg) {
                    problemTypeData("#problemType", msg.data)
                },
                error: function(e) {
                    if (e.status == 500) {
                        layer.msg("系统错误");
                        return
                    } else if (e.responseText) {
                        var errorMessage = JSON.parse(e.responseText).errors[0].message;
                        layer.msg(errorMessage);
                    }
                }
            });
            // 

        },
        error: function(e) {
            if (e.status == 500) {
                layer.msg("系统错误");
                return
            } else if (e.responseText) {
                var errorMessage = JSON.parse(e.responseText).errors[0].message;
                layer.msg(errorMessage);
            }
        }
    });

    // 第一表单附件
    $.ajax({
        type: "get",
        url: "/operation/submitProcess",
        dataType: "json",
        data: { "eventId": defaultParams.eventId },
        cache: false,
        success: function(data) {
            var str = ""
            for (var i = 0; i < data.data.length; i++) {
                var dealFileName = data.data[i].fileName
                str += '  <a fileId="' + data.data[i].eventFlowFileId + '"  data-toggle="tooltip" data-placement="top" title="' + dealFileName + '" class="attachFile col-md-3 col-sm-3 col-xs-3  btn btn-success"  href="/operation/event/flow/file/download?fileId=' + data.data[i].eventFlowFileId + '">' + dealFileName + '</a>'

            }
            $(".attachFileBoxShow1").html(str)
            $("[data-toggle='tooltip']").tooltip()
        },
        error: function(e, textStatus, errorThrown) {
            if (e.status == 500) {
                layer.msg("系统错误");
                return
            } else if (e.responseText) {
                var errorMessage = JSON.parse(e.responseText).errors[0].message;
                layer.msg(errorMessage);
            }
        }
    })

    // 第2表单附件
    $.ajax({
        type: "get",
        url: "/operation/event/flow/file/curentProcess",
        dataType: "json",
        data: { "eventId": defaultParams.eventId },
        cache: false,
        success: function(data) {
            var str = ""
            for (var i = 0; i < data.data.length; i++) {
                var dealFileName = data.data[i].fileName
                str += ' <a fileId="' + data.data[i].eventFlowFileId + '"  data-toggle="tooltip" data-placement="top" title="' + dealFileName + '" class="attachFile col-md-3 col-sm-3 col-xs-3  btn btn-success" href="/operation/event/flow/file/download?fileId=' + data.data[i].eventFlowFileId + '">' + dealFileName + '</a>'
            }
            $(".attachFileBoxShow2").html(str)
        },
        error: function(e) {
            if (e.status == 500) {
                layer.msg("系统错误");
                return
            } else if (e.responseText) {
                var errorMessage = JSON.parse(e.responseText).errors[0].message;
                layer.msg(errorMessage);
            }
        }
    })

    // 第二表单信息
    $.ajax({
            type: "get",
            url: "/operation/event/pending/transformInfo",
            dataType: "json",
            data: { "eventId": defaultParams.eventId },
            cache: false,
            success: function(msg) {


                if (msg.data.isSupportTransform == "0") { //不可分发
                    $("#form2 .form-group").eq(0).hide()
                    $("#problemDeleve").hide()
                } else {
                    $("#form2 .form-group").eq(0).show()
                    $("#problemDeleve").show()
                    $("#distributeMan").val(msg.data.transformUserName)
                    $("#distributePart").val(msg.data.orgName)
                    $("#distributeNum").val(msg.data.tel)
                }
                $("#dealMan").val(msg.data.processUserName)
                if (msg.data.updateTime) {
                    $("#dealTime").val(moment(msg.data.updateTime).format('YYYY-MM-DD HH:mm:ss'))
                } else {
                    $("#dealTime").val("")
                }

                $("#dealSuggest").val(msg.data.processAdvice)
            },
            error: function(e) {
                if (e.status == 500) {
                    layer.msg("系统错误");
                    return
                } else if (e.responseText) {
                    var errorMessage = JSON.parse(e.responseText).errors[0].message;
                    layer.msg(errorMessage);
                }
            }
        })
        //获取组织

    $.ajax({
            type: "get",
            url: "/operation/event/pending/getOrgs",
            dataType: "json",
            data: { "eventId": defaultParams.eventId },
            cache: false,
            success: function(msg) {
                var orgStr = ""
                for (var i = 0; i < msg.data.length; i++) {
                    orgStr += " <option value='" + msg.data[i].orgId + "''>" + msg.data[i].orgName + "</option>"
                }
                $("#departChooseVal").html(orgStr)

            },
            error: function(e) {
                if (e.status == 500) {
                    layer.msg("系统错误");
                    return
                } else if (e.responseText) {
                    var errorMessage = JSON.parse(e.responseText).errors[0].message;
                    layer.msg(errorMessage);
                }
            }
        })
        // 第三部分表单数据
    $.ajax({
            type: "get",
            url: "/operation/event/processed/solveInfo",
            dataType: "json",
            data: { "eventId": defaultParams.eventId },
            cache: false,
            success: function(msg) {
                if (msg.data.showEnded) {
                    $("#form3").show()
                    $(".lineThrough").show()
                } else {
                    $("#form3").hide()
                    $(".lineThrough").hide()
                }
                if (msg.data.insertUserName) {

                    if (msg.data.solve) {
                        var html = ""
                        html += '  <label class="radio-inline">'
                        html += '     <input type="radio" name="inlineRadioOptions" checked id="inlineRadio1" value="true">是'
                        html += ' </label>'
                        html += ' <label class="radio-inline">'
                        html += '     <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="false">否'
                        html += ' </label>'
                    } else {
                        var html = ""
                        html += '  <label class="radio-inline">'
                        html += '     <input type="radio" name="inlineRadioOptions"  id="inlineRadio1" value="true">是'
                        html += ' </label>'
                        html += ' <label class="radio-inline">'
                        html += '     <input type="radio" name="inlineRadioOptions" checked id="inlineRadio2" value="false">否'
                        html += ' </label>'
                    }
                    $("#isSolve").html(html)
                    $("#submitMan").val(msg.data.insertUserName)
                    $("#moreDeac").val(msg.data.remark)
                    $("#moreDeac").attr("disabled", true)
                    $("input[name='inlineRadioOptions']").attr("disabled", true)
                } else {
                    var html = ""
                    html += '  <label class="radio-inline">'
                    html += '     <input type="radio" name="inlineRadioOptions" checked id="inlineRadio1" value="true">是'
                    html += ' </label>'
                    html += ' <label class="radio-inline">'
                    html += '     <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="false">否'
                    html += ' </label>'
                    $("#isSolve").html(html)
                    $("#isSolve").attr("disabled", true)
                    $("#moreDeac").val("")
                    $("#moreDeac").attr("disabled", false)
                    $("#submitMan").val(localStorage.getItem("userName"))

                }
            },
            error: function(e) {
                if (e.status == 500) {
                    layer.msg("系统错误");
                    return
                } else if (e.responseText) {
                    var errorMessage = JSON.parse(e.responseText).errors[0].message;
                    layer.msg(errorMessage);
                }
            }
        })
        // 操作记录
    $("#table2").bootstrapTable('destroy');
    $('#table2').bootstrapTable({
        striped: true, //隔行变色
        showHeader: true,
        escape:true,
        ajax: function(request) {
            $.ajax({
                type: "get",
                url: "/operation/event/flow/",
                dataType: "json",
                data: { "eventId": defaultParams.eventId },
                cache: false,
                success: function(msg) {
                    for (var i = 0; i < msg.data.length; i++) {
                        msg.data[i]["updateTime"] = moment(msg.data[i]["updateTime"]).format('YYYY-MM-DD HH:mm:ss')

                        if (msg.data[i]["status"] == "11") {
                            msg.data[i]["status"] = "已保存"
                        } else if (msg.data[i]["status"] == "12") {
                            msg.data[i]["status"] = "已提交"
                        } else if (msg.data[i]["status"] == "13") {
                            msg.data[i]["status"] = "已回退"
                        } else if (msg.data[i]["status"] == "21") {
                            msg.data[i]["status"] = "待处理"
                        } else if (msg.data[i]["status"] == "22") {
                            msg.data[i]["status"] = "已分发"
                        } else if (msg.data[i]["status"] == "23") {
                            msg.data[i]["status"] = "待确认"
                        } else if (msg.data[i]["status"] == "30") {
                            msg.data[i]["status"] = "已处理"
                        } else if (msg.data[i]["status"] == "90") {
                            msg.data[i]["status"] = "已解决"
                        } else if (msg.data[i]["status"] == "91") {
                            msg.data[i]["status"] = "未解决"
                        }
                    }
                    request.success({
                        row: msg.data
                    });
                    $('#table2').bootstrapTable('load', msg.data);
                },
                error: function(e) {
                    if (e.status == 500) {
                        layer.msg("系统错误");
                        return
                    } else if (e.responseText) {
                        var errorMessage = JSON.parse(e.responseText).errors[0].message;
                        layer.msg(errorMessage);
                    }
                }
            });
        },
        columns: [
            { field: 'eventFlowId', title: 'id', align: "center", width: "20%" },
            { field: 'updateTime', title: '提交时间', align: "center" },
            { field: 'updateUserName', title: '提交人', align: "center" },
            { field: 'status', title: '当前状态', align: "center" },
            { field: 'processAdvice', title: '处理意见', align: "center" }
        ],
        onLoadSuccess: function() {}

    });
}
//删除搜索关键字
function removeSearchVal() {
    $("#searchVal").val("")
    $("#searchVal").focus()
}
//enter搜索
function noNumbers(e) {
    var keynum;
    var keychar;

    keynum = window.event ? e.keyCode : e.which;
    if (keynum == "13") {
        search()
    }
}
//清空默认 
function delDefault(e) {
    if (e.value == "可支持事件编号、事件主题、提交时间、处理人、状态搜索") {
        e.value = ""
    }
}
//移除焦点默认添加
function showTips(e) {
    if (e.value.replace(/^\s*|\s*$/g, "")) {} else {
        e.value = "可支持事件编号、事件主题、提交时间、处理人、状态搜索"
    }
}


function search() {
    var searchval = $("#searchVal").val().indexOf("可支持事件编号、事件主题、提交时间、处理人、状态搜索") > -1 ? "" : $("#searchVal").val();
    $("#exportList").attr("href", encodeURI("/operation/event/processed/export?menuId=" + menuId + "&" + "search=" + searchval))
    defaultParams = ""
    $("#table").bootstrapTable('destroy');
    tableData.initData()
}
//问题类别数据
function problemTypeData(id, arr) {
    // 根据系统通道约束
    var str = ""
    for (var i = 0; i < arr.length; i++) {
        if (arr[i]["questionName"] == defaultParams.questionName) {
            str += '  <option channelid="' + arr[i]["channelId"] + '" menuid="' + arr[i]["menuId"] + '" questionid="' + arr[i]["questionId"] + '"  selected value="' + arr[i]["questionName"] + '">' + arr[i]["questionName"] + '</option>'
        } else {
            str += '  <option channelid="' + arr[i]["channelId"] + '" menuid="' + arr[i]["menuId"] + '" questionid="' + arr[i]["questionId"] + '"  value="' + arr[i]["questionName"] + '">' + arr[i]["questionName"] + '</option>'
        }
    }
    $(id).html(str)
}

// 系统通道数据
function systermRouteData(id, arr) {
    var str = ''
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].channelName === defaultParams.channelName) {
            str += '  <option selected channelId="' + arr[i]['channelId'] + '">' + arr[i]['channelName'] + '</option>'
        } else {
            str += '  <option channelId="' + arr[i]['channelId'] + '">' + arr[i]['channelName'] + '</option>'
        }
    }
    $(id).html(str)
}
